<template>
  <div class="login-com">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form ref="loginchongzhi" :model="loginform" :rules="loginformrules" label-width="0px" class="login-fom">
        <el-form-item prop="username">
          <el-input v-model="loginform.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginform.password" type="password" prefix-icon="el-icon-unlock" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!-- <h1>{{loginform.username}}</h1> -->
          <el-button type="primary" @click="loginto">登录</el-button>
          <el-button type="info" @click="zhongzhibtn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginform: {
        username: 'admin',
        password: '123456'
      },
      loginformrules: {
        username: [
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' },
          { required: true, message: '用户名不能为空！', trigger: 'blur' }
        ],
        password: [
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
          { required: true, message: '密码不能为空！', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    zhongzhibtn () {
      this.$refs.loginchongzhi.resetFields()
    },
    loginto () {
      this.$refs.loginchongzhi.validate(async (bof) => {
        if (!bof) return
        const { data } = await this.$http.post('login', this.loginform)
        if (data.meta.status !== 200) return this.$msg.error('登录失败')
        this.$msg.success('登录成功')
        // console.log(data)
        window.sessionStorage.setItem('token', data.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-com {
  background-color: aquamarine;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar-box {
    width: 130px;
    height: 130px;
    border: 3px solid #eee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 5px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: aquamarine;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-fom{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
        .btns{
            display: flex;
            justify-content: flex-end;
        }
    }
}
</style>
